# coding=utf-8

import web

import settings
import utils
from config import menu
from forms import administrator_login_form
from administrators import AdminSession, auth_login
from administrators.models import admin_login

render = settings.RENDER

class index(object):
    @auth_login
    def GET(self):
        return render.admin.index(menulist = menu)

class home(object):
    @auth_login
    def GET(self):
        return render.admin.home()

class login(object):
    def GET(self):
        return render.admin.login()

    def POST(self):
        f = administrator_login_form()
        if not f.validates():
            # 表单验证失败
            return render.admin.login(form = f)
        else:
            # 表单验证成功
            # 尝试登录
            result = admin_login(f.username.value
                    , f.password.value)
            if result == 'OK':
                # 登录成功
                web.seeother('/')
            else:
                # 登录失败
                return render.admin.login(error = result)

class logout(object):
    '退出登录'
    def GET(self):
        return AdminSession.logout()
